import 'package:imove/model/home_m.dart';

class Result<T>{
  int code;
  String message;//msg
  dynamic data;//原始数据
  bool showMsg;
  T model;
  bool get isSucress => code == 0;
  List<T> listModel = [];//模型数组
  Result({this.code = -1,this.data,this.message,this.showMsg = true});
  fixModel(){
    if(isSucress && data != null){
      if(data is Map && data['result']!=null){
      //数组类型
        List result = data['result'];
        this.listModel.clear();
        result.forEach((element) {
          this.listModel.add(ModelFactory.generateOBJ(element));
        });
      }else{
        this.model = ModelFactory.generateOBJ<T>(data);
      }
    }
  }
}


class BaseResult{
  int code;
  String message;//msg
  dynamic data;//原始数据
  bool showMsg;
  bool get isSucress => code == 0;
  BaseResult({this.code = -1,this.data,this.message,this.showMsg});
  fixModel(){
    
  }
}

class ModelResult<T> extends BaseResult{
  T model;// 模型
   List<T> listModel;//模型数组
  @override
  fixModel() {
    if(isSucress && data != null){
      if(data is Map && data['result']!=null){
      //数组类型
        List result = data['result'];
        this.listModel = [];
        result.forEach((element) {
          this.listModel.add(ModelFactory.generateOBJ(element));
        });
      }else{
        this.data = ModelFactory.generateOBJ<T>(data);
      }
    }
  }
}

class ModelFactory {
  static T generateOBJ<T>(json) {
    if (T.toString() == 'HomeM') {
      return HomeM.fromJson(json) as T;
    //   return OrderM.fromJson(json) as T;
    // }else if(T.toString() == 'CashM'){
    //   return CashM.fromJson(json) as T;
    // }else if(T.toString() == 'AgentM'){
    //   return AgentM.fromJson(json) as T;
    // }else if(T.toString() == 'HotelM'){
    //   return HotelM.fromJson(json) as T;
    // }else if(T.toString() == 'RationM'){
    //   return RationM.fromJson(json) as T;
    // }else if(T.toString() == 'RoomM'){
    //   return RoomM.fromJson(json) as T;
    // }else if(T.toString() == 'GoodsM'){
    //   return GoodsM.fromJson(json) as T;
    // }else if(T.toString() == 'InvoiceM'){
    //   return InvoiceM.fromJson(json) as T;
    // }else if(T.toString() == 'InvoiceGoodsM'){
    //   return InvoiceGoodsM.fromJson(json) as T;
    // }else if(T.toString() == 'DeviceM'){
    //   return DeviceM.fromJson(json) as T;
    // }else if(T.toString() == 'DeviceGridM'){
    //   return DeviceGridM.fromJson(json) as T;
    // }else if(T.toString() == 'StockM'){
    //   return StockM.fromJson(json) as T;
    // }else if(T.toString() == 'StaffM'){
    //   return StaffM.fromJson(json) as T;
    // }else if(T.toString() == 'ApplyM'){
    //   return ApplyM.fromJson(json) as T;
    // }else if(T.toString() == 'ChannelM'){
    //   return ChannelM.fromJson(json) as T;
    }else{
    
      return json;
    }
    
  }
}
